/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.support;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.core.metrics.StartupStep;
import org.springframework.lang.Nullable;

import java.util.*;

/**
 * Delegate for AbstractApplicationContext's post-processor handling.
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 4.0
 */
final class PostProcessorRegistrationDelegate {

	private PostProcessorRegistrationDelegate() {
	}


	public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// WARNING: Although it may appear that the body of this method can be easily
		// refactored to avoid the use of multiple loops and multiple lists, the use
		// of multiple lists and multiple passes over the names of processors is
		// intentional. We must ensure that we honor the contracts for PriorityOrdered
		// and Ordered processors. Specifically, we must NOT cause processors to be
		// instantiated (via getBean() invocations) or registered in the ApplicationContext
		// in the wrong order.
		//
		// Before submitting a pull request (PR) to change this method, please review the
		// list of all declined PRs involving changes to PostProcessorRegistrationDelegate
		// to ensure that your proposal does not result in a breaking change:
		// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

		/**
		 * 前提是: 这些 bean definition 都不能是通过 API 方式提供的
		 * 存放已经处理完的 BeanFactoryPostProcessor 以及他的子类 BeanDefinitionRegistryPostProcessor
		 * 防止重复执行
		 */
		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();

		// 只要没对 spring 源码进行修改，则默认都会进入该 if
		if (beanFactory instanceof BeanDefinitionRegistry) {
			// 为了进行后续的注册 BeanFactoryPostProcessor,所以需要类型转换
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

			// 存储所有直接实现了父类 BeanFactoryPostProcessor
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();

			// 存储所有直接实现了子类 BeanDefinitionRegistryPostProcessor
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			// 针对 api 提供的 BeanFactoryPostProcessor 处理
			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				// 实现子类的处理
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
					// 回调实现了子类的类中的方法
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					// 添加
					registryProcessors.add(registryProcessor);
				}
				// 实现父类的处理
				else {
					// 添加
					regularPostProcessors.add(postProcessor);
				}
			}


			// Do not initialize FactoryBeans here: We need to leave all regular beans
			// uninitialized to let the bean factory post-processors apply to them!
			// Separate between BeanDefinitionRegistryPostProcessors that implement
			// PriorityOrdered, Ordered, and the rest.
			// 存储通过 非API 方式注册的 子类 BeanDefinitionRegistryPostProcessor
			List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

			// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
			// 根据类型到 beanDefinitionMap 中查找,返回名字，而当前的 容器还没有初始化完成，那么beanDefinitionMap 中仅有自己的配置类和那5个spring 初始化的时候的核心类
			String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				// 是否实现了 PriorityOrdered 接口
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					// 实例化并且放到当前需要执行的集合当中
					// beanFactory.getBean 会去实例化 ConfigurationClassPostProcessor 且存放到单例池里面
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					// 表示已经处理完了
					processedBeans.add(ppName);
				}
			}

			// 排序,spring5.3.x 中默认的beanDefinitionMap 5个核心类中，只有一个是 子类的实现，即 ConfigurationClassPostProcessor，那么这里就不需要关心排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);

			// 合并到 子类 list 中去,为了将来一起执行父类方法，即为了将来执行 BeanFactoryPostProcessor.postProcessBeanFactory
			registryProcessors.addAll(currentRegistryProcessors);

			/**
			 *  遍历执行，即执行 BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
			 *  这里仅会执行 {@link org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry(BeanDefinitionRegistry)}
			 *  也就以为这，这里就开始 @Import 注解的解析 、配置类的扫包、半配置类和全配置类的标记
			 */
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());

			// 清空这个集合因为 因为下一次还要用
			currentRegistryProcessors.clear();


			// 由于执行了 ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry 方法完成了扫描， beanFactory 中已经发生了变化
			// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				// 没有处理过的 且 实现了 Ordered 接口的 子类处理
				if (! processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			// 排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			// 合并到 子类 list 中去,为了将来一起执行父类方法，即为了将来执行 BeanFactoryPostProcessor.postProcessBeanFactory
			registryProcessors.addAll(currentRegistryProcessors);
			// 遍历执行，即执行 BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
			// 清空这个集合因为 因为下一次还要用
			currentRegistryProcessors.clear();


			/**
			 * 最后，调用所有其他BeanDefinitionRegistryPostProcessors，直到没有其他BeanDefinitionRegistryPostProcessors出现为止。
			 * 这里为什么还要再执行依次呢，因为实现了子类 BeanDefinitionRegistryPostProcessor 的类中 有可能使用 postProcessBeanDefinitionRegistry 方法又注入了 其他的 BeanDefinition
			 * 如：
			 *        @Override
			 *    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
			 * 		log.debug("c-s api subclass postProcessBeanDefinitionRegistry");
			 * 		BeanDefinitionBuilder x= BeanDefinitionBuilder.genericBeanDefinition(X.class);
			 * 		registry.registerBeanDefinition("x",x.getBeanDefinition());
			 *    }
			 */
			// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
			boolean reiterate = true;
			// 默认死循环
			while (reiterate) {
				reiterate = false;
				postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
				for (String ppName : postProcessorNames) {
					if (! processedBeans.contains(ppName)) {
						currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
						processedBeans.add(ppName);
						reiterate = true;
					}
				}
				// 排序
				sortPostProcessors(currentRegistryProcessors, beanFactory);
				// 合并到 子类 list 中去,为了将来一起执行父类方法，即为了将来执行 BeanFactoryPostProcessor.postProcessBeanFactory
				registryProcessors.addAll(currentRegistryProcessors);
				//  遍历执行，即执行 BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
				invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
				// 清空这个集合 后续不再使用
				currentRegistryProcessors.clear();
			}


			// 现在，调用到目前为止处理过的所有处理器的postProcessBeanFactory回调。
			// Now, invoke the postProcessBeanFactory callback of all processors handled so far.

			// 实现了子类的父类方法
			invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
			// 执行通过 api 提供的直接实现了父类的 方法
			invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
		} else {
			// 这个一般不会进,就不用分析了
			// Invoke factory processors registered with the context instance.
			invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
		}

		/**
		 * 这里不要初始化FactoryBeans:我们需要保留所有常规bean
		 * 未初始化以让bean工厂后处理器应用于它们!
		 * 找到所有不是通过 API 方式添加进来的 父类
		 */
		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let the bean factory post-processors apply to them!
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

		// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		// 实现了 priorityOrdered 的父类集合，这里放到是实例
		List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		// 实现了 order 接口的父类集合
		List<String> orderedPostProcessorNames = new ArrayList<>();
		// 没有任何实现
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();

		// 根据各种实现接口进行筛选
		for (String ppName : postProcessorNames) {
			if (processedBeans.contains(ppName)) {
				// skip - already processed in first phase above
			} else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				// 实现了 PriorityOrdered 接口处理
				// 注意: 整个 for 循环中，仅有实现了 PriorityOrdered 接口的才会进行 实例化，即调用 beanFactory.getBean 方法 ，为什么这样做呢？
				// 因为 这里涉及到优先级的问题，优先级高的就先执行了，避免后面执行的时候不生效
				priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
			} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				// 直接实现了 Ordered 接口处理
				orderedPostProcessorNames.add(ppName);
			} else {
				// 没有任何实现处理
				nonOrderedPostProcessorNames.add(ppName);
			}
		}


		// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);


		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
		for (String postProcessorName : orderedPostProcessorNames) {
			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);


		// Finally, invoke all other BeanFactoryPostProcessors.
		List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
		for (String postProcessorName : nonOrderedPostProcessorNames) {
			nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
		}
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);


		// Clear cached merged bean definitions since the post-processors might have
		// modified the original metadata, e.g. replacing placeholders in values...
		beanFactory.clearMetadataCache();
	}

	/**
	 * 该方法的目的就是为了查找和注册  BeanPostProcessors
	 *
	 * @param beanFactory
	 * @param applicationContext
	 */
	public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

		// WARNING: Although it may appear that the body of this method can be easily
		// refactored to avoid the use of multiple loops and multiple lists, the use
		// of multiple lists and multiple passes over the names of processors is
		// intentional. We must ensure that we honor the contracts for PriorityOrdered
		// and Ordered processors. Specifically, we must NOT cause processors to be
		// instantiated (via getBean() invocations) or registered in the ApplicationContext
		// in the wrong order.
		//
		// Before submitting a pull request (PR) to change this method, please review the
		// list of all declined PRs involving changes to PostProcessorRegistrationDelegate
		// to ensure that your proposal does not result in a breaking change:
		// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

		/**
		 * 根据类型到 beanDefinitionMap 中查找,返回名字
		 */
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);


		// Register BeanPostProcessorChecker that logs an info message when
		// a bean is created during BeanPostProcessor instantiation, i.e. when
		// a bean is not eligible for getting processed by all BeanPostProcessors.

		// 目标期望的数量 即 spring 期望 bean 应该执行几个 BeanPostProcessor 的数量
		int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;

		// 这里是预期值，还不是实际的，因为这里的 BeanPostProcessor 仅仅是 spring 认为的将来有一天会放到这个 list 当中的 bean 做后置处理，但是现在还没有放进去
		// 那些情况会出现预期和实际不符呢？
		// 1.在添加完 BeanPostProcessorChecker 之后，其中 PostProcessorRegistrationDelegate.BeanPostProcessorChecker.postProcessAfterInitialization 方法在做校验的具体步骤
		// 2.在 beanPostProcessors 没有添加到预期的所有对象之前，有了一个 bean 开始走生命周期
		beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));


		// Separate between BeanPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
		List<String> orderedPostProcessorNames = new ArrayList<>();
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();

		/**
		 * 遍历从 beanDefinitionMap 中获得的 BeanPostProcessor并根据父类不同进行分组
		 * 并 调用实例化方法
		 */
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				// 这里就完成了实例化!!!!!! 并不会调用 BeanPostProcessorChecker 去进行校验，
				// 因为 PostProcessorRegistrationDelegate.BeanPostProcessorChecker.postProcessAfterInitialization 这里有判断
				BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

				// 添加到缓存中
				priorityOrderedPostProcessors.add(pp);

				// 再分一次组
				if (pp instanceof MergedBeanDefinitionPostProcessor) {
					internalPostProcessors.add(pp);
				}
			} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			} else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		// First, register the BeanPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);

		// 注册 beanPostProcessors   对象到 beanPostProcessors list 中去
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		/**
		 * 针对实现了 Ordered 接口的 beanPostProcessor 进行处理
		 * 1.实例化
		 */
		// Next, register the BeanPostProcessors that implement Ordered.
		List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
		for (String ppName : orderedPostProcessorNames) {
			// 进行实例化
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

			// 将实例完毕的对象，放入到  orderedPostProcessors list 中，供下一步存储到  beanPostProcessors list 中去
			orderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, orderedPostProcessors);


		/**
		 * 针对没有实现任何接口的 beanPostProcessor 进行处理
		 * 1.实例化
		 */
		// Now, register all regular BeanPostProcessors.
		List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
		for (String ppName : nonOrderedPostProcessorNames) {
			// 进行实例化，这里必然会进行属性填充
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			nonOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);


		// Finally, re-register all internal BeanPostProcessors.
		sortPostProcessors(internalPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, internalPostProcessors);


		// Re-register post-processor for detecting inner beans as ApplicationListeners,
		// moving it to the end of the processor chain (for picking up proxies etc).
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
	}

	private static void sortPostProcessors(List<?> postProcessors, ConfigurableListableBeanFactory beanFactory) {
		// Nothing to sort?
		if (postProcessors.size() <= 1) {
			return;
		}
		Comparator<Object> comparatorToUse = null;
		if (beanFactory instanceof DefaultListableBeanFactory) {
			comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
		}
		if (comparatorToUse == null) {
			comparatorToUse = OrderComparator.INSTANCE;
		}
		postProcessors.sort(comparatorToUse);
	}

	/**
	 * Invoke the given BeanDefinitionRegistryPostProcessor beans.
	 */
	private static void invokeBeanDefinitionRegistryPostProcessors(Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry, ApplicationStartup applicationStartup) {

		for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
			StartupStep postProcessBeanDefRegistry = applicationStartup.start("spring.context.beandef-registry.post-process").tag("postProcessor", postProcessor::toString);

			// 执行子类 BeanDefinitionRegistryPostProcessor 中的  postProcessBeanDefinitionRegistry 方法,
			// 里面执行的内容多了去了
			postProcessor.postProcessBeanDefinitionRegistry(registry);

			postProcessBeanDefRegistry.end();
		}
	}

	/**
	 * 子类完成了 全配置类和半配置类的标识 ，父类再来处理
	 *
	 * Invoke the given BeanFactoryPostProcessor beans.
	 */
	private static void invokeBeanFactoryPostProcessors(Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

		for (BeanFactoryPostProcessor postProcessor : postProcessors) {
			StartupStep postProcessBeanFactory = beanFactory.getApplicationStartup().start("spring.context.bean-factory.post-process").tag("postProcessor", postProcessor::toString);
			// 父类处理全配置类和半配置类
			postProcessor.postProcessBeanFactory(beanFactory);
			postProcessBeanFactory.end();
		}
	}

	/**
	 * Register the given BeanPostProcessor beans.
	 */
	private static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

		if (beanFactory instanceof AbstractBeanFactory) {
			// Bulk addition is more efficient against our CopyOnWriteArrayList there
			((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);
		} else {
			for (BeanPostProcessor postProcessor : postProcessors) {
				beanFactory.addBeanPostProcessor(postProcessor);
			}
		}
	}


	/**
	 * BeanPostProcessor that logs an info message when a bean is created during
	 * BeanPostProcessor instantiation, i.e. when a bean is not eligible for
	 * getting processed by all BeanPostProcessors.
	 */
	private static final class BeanPostProcessorChecker implements BeanPostProcessor {

		private static final Log logger = LogFactory.getLog(BeanPostProcessorChecker.class);

		private final ConfigurableListableBeanFactory beanFactory;

		private final int beanPostProcessorTargetCount;

		public BeanPostProcessorChecker(ConfigurableListableBeanFactory beanFactory, int beanPostProcessorTargetCount) {
			this.beanFactory = beanFactory;
			this.beanPostProcessorTargetCount = beanPostProcessorTargetCount;
		}

		@Override
		public Object postProcessBeforeInitialization(Object bean, String beanName) {
			return bean;
		}

		/**
		 * spring 内置的后置处理器 用于校验实例化的 bean 是否都完成了  应该执行几个 BeanPostProcessor 的数量，这是一个回调方法
		 *
		 * @param bean the new bean instance
		 * @param beanName the name of the bean
		 * @return
		 */
		@Override
		public Object postProcessAfterInitialization(Object bean, String beanName) {
			// 这里就有判断，表明不会 BeanPostProcessor 进行校验
			if (! (bean instanceof BeanPostProcessor) && ! isInfrastructureBean(beanName) && this.beanFactory.getBeanPostProcessorCount() < this.beanPostProcessorTargetCount) {
				if (logger.isInfoEnabled()) {
					logger.info("Bean '" + beanName + "' of type [" + bean.getClass().getName() + "] is not eligible for getting processed by all BeanPostProcessors " + "(for example: not eligible for auto-proxying)");
				}
			}
			return bean;
		}

		private boolean isInfrastructureBean(@Nullable String beanName) {
			if (beanName != null && this.beanFactory.containsBeanDefinition(beanName)) {
				BeanDefinition bd = this.beanFactory.getBeanDefinition(beanName);
				return (bd.getRole() == RootBeanDefinition.ROLE_INFRASTRUCTURE);
			}
			return false;
		}
	}

}
